-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(v2): add ability to set custom heading id #4222
Conversation
c8baed6
to
477f43d
Compare
[V1] Deploy preview success Built with commit c8baed6 |
Size Change: +543 B (0%) Total Size: 533 kB
ℹ️ View Unchanged
|
Deploy preview for docusaurus-2 ready! Built with commit c8baed6 |
⚡️ Lighthouse report for the changes in this PR:
Lighthouse ran on https://deploy-preview-4222--docusaurus-2.netlify.app/classic/ |
[V1] Deploy preview success Built with commit 728d262 |
Deploy preview for docusaurus-2 ready! Built with commit 728d262 |
477f43d
to
5b6a4dc
Compare
// Support explicit heading IDs | ||
const customHeadingIdMatches = customHeadingIdRegex.exec(heading); | ||
|
||
if (customHeadingIdMatches) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious if this feature will have to be configurable? How much of an impact on performance would it have if it were always enabled by default? Or does it make sense to create a separate plugin for this feature? For now I have combined it into the existing slug plugin because they essentially do the same thing (create headings id).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to enable by default and put in mdx loader.
We likely want to encourage users to have stable anchor ids and this should work consistently for docs, blog and pages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just thought that having new plugin might be better for this. Since this feature is mainly needed when using the i18n. Moreover, I think to add a cli command to automatically create explicit ids for existing headers (we have extendCli
hook in plugin lifecycle). Because currently I do not understand how I can add a new cli command inside the mdx-loader
package. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a way to "lock" the ids at a given time looks like a nice idea.
Not sure how to build that easily, how would the plugin know where to look for markdown files, as the paths are provided as options to the docs, blog, page plugin?
The only thing I'm thinking of is having this cli in core, and getting all the relevant paths from getPathsToWatch
(as you basically watch the md files, it will work but feel a bit hacky at the same time)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this feature is mainly needed when using the i18n.
I think it's actually useful in all cases. If you want to link to some heading, we'd rather ensure that link does not break when the heading is updated.
Fixing heading ids you want to link to should rather be the norm, and we could even add a warning someday to tell the user that he's linking to an anchor that has not been fixed?
Currently it's too easy to have broken anchor links in a Docusaurus site.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I lost sight of that it is currently not possible to add new Remark plugin from the custom Docusaurus plugin. Maybe we should create new hook for this purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to add new command to write heading ids to the docusaurus core (eg. yarn docusaurus write-heading-ids community
). Its implementation is very simple, so it might be worth improving it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this, that will be really useful for i18n!
|
||
const visit = require('unist-util-visit'); | ||
const toString = require('mdast-util-to-string'); | ||
const slugs = require('github-slugger')(); | ||
|
||
const customHeadingIdRegex = /^(.*?)\s*\{#([\w-]+)\}$/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about extracting customHeadingIdRegex.exec(heading);
in a separate function const {heading,id} = extractHeadingId(rawHeading)
and adding tests?
That would simplify the testing of Regepx edge cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, but we have separate test that with different cases, so it seems to me redundant extract new function (and its test), because in many ways it will duplicate this test.
// Support explicit heading IDs | ||
const customHeadingIdMatches = customHeadingIdRegex.exec(heading); | ||
|
||
if (customHeadingIdMatches) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to enable by default and put in mdx loader.
We likely want to encourage users to have stable anchor ids and this should work consistently for docs, blog and pages.
|
||
### Custom id headers {#custom-id} | ||
|
||
With `{#custom-id}` syntax you can set your own header id. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth creating a page dedicated to anchor links in markdown features category?
I'd also document this in the i18n section but I can do it in another PR?
@slorber I just tried this out by calling |
@krillboi can you try on the doc site folder directly instead of using --cwd ? |
@slorber I can try that. I am currently using |
@slorber I tried this instead, executed from the site directory: |
😅 I mean can you try cding into the directory and just running What's your site repo? a branch to inspect? |
@slorber I don't have a global installation of node. Haven't had trouble with using --cwd until now for all the other docusaurus commands like If I write some invalid path to siteDir like:
Is it recommended to upgrade to yarn 2? |
So I've tried logging the output of the command and
.mdx files. I'll investigate further.
|
Still haven't fixed it but have a suspicion that it has something to do with: sindresorhus/globby#155 |
@slorber So replacing the globby call with the following has worked for me (taking inspiration from some of your other functions):
It's not perfect as, for example, all the |
Oh, I see you are on windows, that definitively look like the reason :) thanks for reporting |
Motivation
Closes #3322
Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
Tests and preview https://deploy-preview-4222--docusaurus-2.netlify.app/classic/examples/markdownPageExample#custom
Related PRs
(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)